package com.ff.discounts.component.freedetail

import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.ff.discounts.R
import com.ff.discounts.databinding.FragmentDialogPayBottomBinding
import com.ff.discounts.dialog.BaseViewModelDialogFragment
import com.ff.discounts.superui.util.ScreenUtil

/**
 *
 * @author: fufang
 * @date: 2023/10/31
 */
class PayBottomDialog : BaseViewModelDialogFragment<FragmentDialogPayBottomBinding>() {
    private lateinit var onAgreementClickListener: View.OnClickListener
    override fun initViews() {
        super.initViews()
        // 点击弹窗外面不能关闭
        isCancelable = true

        // 设置支付组件
        binding.composePay.setContent {
            Column(
                modifier = Modifier
                    .background(color = Color.White)
                    .fillMaxWidth(1f)
                    .clip(shape = RoundedCornerShape(20))
                    .padding(start = 23.dp, end = 23.dp, top = 20.dp, bottom = 20.dp)
                ,
            ) {
                Row(
                    horizontalArrangement = Arrangement.SpaceBetween,
                    modifier = Modifier.fillMaxWidth(1f).padding(bottom = 20.dp)
                ) {
                    Row() {
                        Image(
                            modifier = Modifier.padding(end = 6.dp),
                            painter = painterResource(id = R.drawable.common_alipay),
                            contentDescription = null
                        )
                        Text(
                            text = "支付宝支付",
                            color = Color(0xff222222),
                            style = TextStyle(
                                fontWeight = FontWeight.W500,
                                fontSize = 13.sp
                            ),
                            modifier = Modifier.padding(end = 6.dp)
                        )
                    }
                    Image(
                        painter = painterResource(id = R.drawable.common_pay_checked),
                        contentDescription = null
                    )
                }

               Row(modifier = Modifier
                   .fillMaxWidth(1f)
                   .height(1.dp)
                   .background(Color(0xFFEDEDED))
                   .padding(20.dp)) {

               }
                Row(
                    horizontalArrangement = Arrangement.SpaceBetween,
                    modifier = Modifier.fillMaxWidth(1f).padding(top = 20.dp)
                ) {
                    Row() {
                        Image(
                            modifier = Modifier.padding(end = 6.dp),
                            painter = painterResource(id = R.drawable.common_wechat),
                            contentDescription = null
                        )
                        Text(
                            text = "微信支付",
                            color = Color(0xff222222),
                            style = TextStyle(
                                fontWeight = FontWeight.W500,
                                fontSize = 13.sp
                            ),

                        )
                    }
                    Image(
                        painter = painterResource(id = R.drawable.common_pay_checked),
                        contentDescription = null
                    )
                }
            }
        }


    }

    override fun initDatum() {
        super.initDatum()

    }

    override fun initListeners() {
        super.initListeners()


    }


    override fun onResume() {
        super.onResume()
        //修改宽度，默认比AlertDialog.Builder显示对话框宽度窄，看着不好看
        //参考：https://stackoverflow.com/questions/12478520/how-to-set-dialogfragments-width-and-height
        val params: WindowManager.LayoutParams = dialog!!.window!!.attributes
        params.width = ScreenUtil.getScreenWith(requireContext())
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT
        // 设置弹窗在底部
        params.gravity = Gravity.BOTTOM
        dialog!!.window!!.attributes = params as WindowManager.LayoutParams
        val drawable = ContextCompat.getDrawable(
            requireContext(), com.ff.discounts.R.drawable.shape_empty
        )
        /// 设置内容背景颜色
        dialog?.window?.setBackgroundDrawable(drawable);

    }

    companion object {
        /**
         * 显示对话框
         */
        fun show(fragmentManager: FragmentManager, onAgreementClickListener: View.OnClickListener) {
            val dialogFragment = PayBottomDialog()


            dialogFragment.show(fragmentManager, "TermServiceDialogFragment")
        }
    }
}